// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
  // item:0,
  tab:0,
  playlist:[{
    id:1,
    title:'夜空中最亮的星',
    singer:'逃跑计划',
    src:'http://172.16.26.249:3000/1.mp3',
    coverImgUrl:'/images/cover.jpg'
  },{
    id:2,
    title:'奏鸣曲',
    singer:'莫扎特',
    src:'http://172.16.26.249:3000/2.mp3',
    coverImgUrl:'/images/cover.jpg'
  },{
    id:3,
    title:'欢乐颂',
    singer:'贝多芬',
    src:'http://172.16.26.249:3000/3.mp3',
    coverImgUrl:'/images/cover.jpg'
  },{
    id:4,
    title:'爱之梦',
    singer:'李斯特',
    src:'http://172.16.26.249:3000/4.mp3',
    coverImgUrl:'/images/cover.jpg'
  }
],
state:'paused',
playIndex:0,
play:{
  currentTime:'00:00',
  duration:'00:00',
  percent:0,
  title:'',
  singer:'',
  coverImagUrl:'/images/cover.jpg',
}
  },

  changeItem:function(e){
     console.log(e.target.dataset.item);
    this.setData({
      item:e.target.dataset.item
    })
  },

  changeTab:function(e){
this.setData({
  tab:e.detail.current
});
  },

onReady:function(){
  this.audioCtx = wx.createAudioContext();
},
audioCtx:null,
onReady:function(){
  // this.audioCtx = wx.createInnerAudioContext()
  this.audioCtx = wx.createInnerAudioContext()
  //默认第一曲
  this.setMusic(0);

  var that = this;
  //播放错误处理
  this.audioCtx.onError(
    function(){
      console.log('播放失败' + that.audioCtx.src);
    }
  );

  //一曲放完后处理
  this.audioCtx.onEnded(
    function(){
      that.next();
    }
  );

  //音乐播放当中
  this.audioCtx.onTimeUpdate(
    function(){

    }
  );

  //音乐播放时间进度跟新
  this.audioCtx.onTimeUpdate(
    function(){
    console.log(that.audioCtx.currentTime+'/'+that.audioCtx.duration)
    that.setData({
      'play.currentTime':formaTime(that.audioCtx.currentTime),
      'play.duration':formaTime(that.audioCtx.duration),
      'play.percent':(that.audioCtx.currentTime/that.audioCtx.duration)*100
    })
  }
  );

  function formaTime(time){
    var minute = Math.floor(time/60)%60;
    var second = Math.floor(time)%60;
    return (minute<10? '0'+minute:minute)+":"+(second<10?'0'+second:second);
  }
},
//音乐播放
setMusic(index){
  var music = this.data.playlist[index];
  console.log(music);
  this.audioCtx.src=music.src;
  this.setData({
    playIndex:index,
    'play.title':music.title,
    'play.singer':music.singer,
    'play.coverImgUrl':music.coverImgUrl,
    'play.currentTime':'00:00',
    'play.duration':'00:00',
    'play.percent':0
  })
},
play:function(){
  this.audioCtx.play();
  this.setData({
    state:'running'
  })
},
pause:function(){
  this.audioCtx.pause();
  this.setData({
    state:'paused'
  })
},

//下一曲按钮
next:function(){
  var index = this.data.playIndex>=this.data.playlist.length-1?0:this.data.playIndex+1;
  this.setMusic(index);
  if(this.data.state==='running'){
    this.play()
  }
},

sliderChange(e){
var second = e.detail.value/100*this.audioCtx.duration;
this.audioCtx.seek(second);
},

change(e){
  var index=e.currentTarget.dataset.index;
  this.setMusic(index);
  this.play();
},

  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
